<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;

class Shop_ordersController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        //
        $num = $request -> input('num', 20);
         $keywords = $request -> input('keywords', '');

         $data = DB::table('shop_goods as c1') 
                -> select('c1.*', 'c2.name as pname')
                -> leftJoin('shop_type as c2', 'c1.tid', '=', 'c2.id')
                -> where('gname', 'like', '%'.$keywords.'%') 
                -> paginate($num);

        return view('home.shop_orders.index', ['data' => $data, 'request' => $request -> all(), 'title' => '商品列表']);
    }

    //查询商品



    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
 
    public function show($id)
    {
        //
        //      $cateData = DB::table('shop_goods') -> where('id', $id) -> first();

        // return view('home.shop_goods.show', ['title' => '商品详情'] ,['data' => $data]);

          $data = DB::table('shop_type as c1') 
        -> select('c1.*', 'c2.name as pname', DB::raw("concat(c1.path, ',', c1.id) as sortpath"))
        -> leftJoin('shop_type as c2', 'c1.pid', '=', 'c2.id')
        -> orderBy('sortpath')
        -> get();

        $cateData = DB::table('shop_goods') -> where('id', $id) -> first();


        foreach($data as $key => $value){
            $num = substr_count($value -> path, ',');
            $str = str_repeat('|---', $num);
            $data[$key] -> name = $str.$value -> name;
        }


        return view('home.shop_orders.show', ['title' => '编辑分类'], ['data' => $data, 'cateData' => $cateData]);
        
    }

 
}
